<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/28
 * Time: 14:11
 */
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class Prize extends Model
{
    /**
     * @param $uid
     * @param $monetary
     * @param $moneyType
     * 面膜提成收入
     */
    public function getPrize( $uid ,$money ,$num,$ordersn,$goodsname)
    {
        $memberBill = PercentageRoles::where('id', 1)->pluck('Royalty_ratio')->first();//会员提成比例
        $memberAmount = $memberBill * $money;//会员身份收入
                //所有会员购买面膜得 总支出的20%
                $member = Members::where('uid', $uid)->first();
                if ($member) {

                            $amount = $memberAmount;
                            Members::where('uid', $member['uid'])
                                ->update([
                                    'credit2' => $member['credit2'] + $amount,
                                    'total' => $member['total'] + $amount,
                                    'num' => $member['num'] + $num,
                                ]);
                          
                    $transaction = [
                        'uid' => $uid,
                        'ordersn' => $ordersn,
                        'money' => $amount,
                        'type' => 5,
                        'time' => time(),
                        'explain' => '购买'.$goodsname.',奖励反比',
                    ];
                    $transaction = RecordTransaction::create($transaction);
                    $priceTransaction = [
                        'uid' => $member['uid'],
                        'tid' => $transaction['id'],
                        'time' =>time(),
                        'money' => $amount,
                        'remark' => '购买'.$goodsname.',奖励反比：'.$amount,
                    ];
                    RecordBonus::create($priceTransaction);

                    }

    }


    /**
     * 区域代理分红
     * @param int $money 支付金额
     * @param int $company_id 分公司id
     * @param array $Poundage 系统设置
     * @param null $province 省
     * @param null $city     市
     * @param null $area     区
     * @param string $ordersn     统一订单
     * @return array
     */
    public function agency($money = 0 ,$province = null , $city = null , $area = null,$uid ,$num,$ordersn,$goodsname)
    {
        if ($money < 0 || empty($money)) {
            return ['code' => 0, 'data' => '参数错误'];
        }
        //查询市代理提成比例
        $userApply = new Apply();
        $agentBill1 = PercentageRoles::where('id', 2)->pluck('Royalty_ratio')->first();//代理提成比例
        $agentAmount1 = $agentBill1 * $money;//市代理提成收入

        //查询该区域市代理
        $userCity = $userApply->where(['type' => 1, 'city' => $city, 'status' => 1])->get()->toArray();
        $agencyUid = '';
        foreach ($userCity as $k => $v) {
            $district = json_decode($v['district'],true);
            if($district){
                $district = explode(",",$district);
                if(in_array($area,$district)){
                    $agencyUid = $userCity[$k]['uid'];

                }
            } else {
                $agencyUid = $v['uid'];
                
            }
        }
        $agency = Members::where('uid',$agencyUid)->first();
        $this->credit2Record( $ordersn, $agency['uid'] );
        if ($agency) {
            //计算金额
            $amount = $agentAmount1;
            Members::where('uid', $agency['uid'])
                ->update([
                    'credit2' => $agency['credit2'] + $amount,
                    'total' => $agency['total'] + $amount,
                    'dnum' => $agency['dnum'] + $num,
                ]);


                $transaction = [
                    'uid' => $agency['uid'],
                    'ordersn' => $ordersn,
                    'money' => $amount,
                    'type' => 5,
                    'time' => time(),
                    'explain' => '购买'.$goodsname.',市代理奖励反比',
                ];
            $transaction = RecordTransaction::create($transaction);
            $priceTransaction = [
                'uid' => $agency['uid'],
                'tid' => $transaction['id'],
                'time' => time(),
                'money' => $amount,
                'remark' =>'购买'.$goodsname.',市代理奖励反比：'.$amount,
            ];
            RecordBonus::create($priceTransaction);
            
        }
    }


    /**
     * 记录订单之前各会员的奖励币余额记录
     * @param int $uid 用户ID
     * @param string $ordersn     统一订单
     * @return array
     */
    public function credit2Record( $ordersn, $uid )
    {
        $ShopOrder = new ShopOrder();
        $UserModel = new Members();
        $orderMemberCredit2Datas = [];
        $orderMemberCredit2Uid = [];
        $orderMemberCredit2Uids = [];
        $memberLastCredit2Data = $ShopOrder->where( [ 'ordersn' => $ordersn ] )->value( 'last_credit2s' );
        if ( $memberLastCredit2Data != "" ) {
            $memberLastCredit2Data = unserialize( $memberLastCredit2Data );
        } else {
            $memberLastCredit2Data = [];
        }
        $memberLastCredit2 = [];
        $memberLastData = $UserModel->where([ 'uid' => $uid ])->select( 'uid', 'name', 'user_name', 'voucher','credit1', 'credit2', 'credit4' )->first();
        $memberLastCredit2['uid'] = $memberLastData['uid'];
        $memberLastCredit2['name'] = $memberLastData['name'];
        $memberLastCredit2['user_name'] = $memberLastData['user_name'];
        $memberLastCredit2['voucher'] = $memberLastData['voucher'];
        $memberLastCredit2['credit1'] = $memberLastData['credit1'];
        $memberLastCredit2['credit2'] = $memberLastData['credit2'];
        $memberLastCredit2['credit4'] = $memberLastData['credit4'];
        if ( $memberLastCredit2Data ) {
            foreach ($memberLastCredit2Data as $key => $value) {
                array_push( $orderMemberCredit2Uid, $value['uid'] );
            }
            if ( !in_array( $uid, $orderMemberCredit2Uid ) ) {
                array_push( $memberLastCredit2Data, $memberLastCredit2 );
            }
            $Credit2SaveData = serialize( $memberLastCredit2Data );
        } else {
            if ( count( $orderMemberCredit2Datas ) > 0 ) {
                foreach ($orderMemberCredit2Datas as $key => $value) {
                    array_push( $orderMemberCredit2Uids, $value['uid'] );
                }
                if ( !in_array( $uid, $orderMemberCredit2Uids ) ) {
                    array_push( $orderMemberCredit2Datas, $memberLastCredit2 );
                }
            } else {
                array_push( $orderMemberCredit2Datas, $memberLastCredit2 );
            }
            $Credit2SaveData = serialize( $orderMemberCredit2Datas );
        }
        $ShopOrder->where( [ 'ordersn' => $ordersn ] )->update( [ 'last_credit2s' => $Credit2SaveData ] );
    }



}